<?php
class CTtwo extends spController
{
	function __construct(){ // 公用
		parent::__construct(); // 这是必须的
		$this->tpl_title = "产品模块|主题部分";
	}
	
	
	function start_detection()
	{
		//这里是开启检测功能
		$this->detection();
		//$this->moniter();
	}
	
	function stop_detection()
	{
		//这里是停止检测功能
	}
	
	//检测
	public function detection() 
	{
		$site = spClass("site");
		$where['active'] = 1;
		$b = $site->findAll($where);
		 
		foreach ($b as $i)
		{		
			$runtime = spClass("runtime");			
 	        $dat_time=date("Y-m-d H:i:s"); 	        
 	        $runtime->query("update baomu_runtime set runtime='$dat_time' where time_id=1"); 
 	        
 	        
 	        //向检测程序发送检测命令和网址         

		}
			
	}
	

    /**
    +----------------------------------------------------------
    * 循环
    +----------------------------------------------------------
    */
    
	private function moniter()
    {    	
    	ignore_user_abort(); // run script in background

        set_time_limit(0); // run script forever

        $interval=100; // do every 15 minutes…循环间隔变量
        
        do
        {        	
			// * 调用检测函数-----------------*/       
			$this->detection();
			
			//----------------------------------    
			sleep($interval); // wait 15 minutes循环间隔（睡眠时间）

		}while(true);
    
      }
	

      
	function start()
	{
		$url = "系统开启成功";
		//.spUrl("main","start_detection");
		//$time = $this->spArgs('interval');
		$id = $this->spArgs('id');
		switch ($id)
		{
			case 1:				
				$time = 60;
				$path="produce.php?c=main&a=start_detection"."&time=".$time;
				if($this->ffp($path))
				{
					echo $url;
					echo $path."这是一分钟检测";
				}
				break;
			case 2:				
				$time = 120;
				$path="produce.php?c=main&a=start_detection"."&time=".$time;
				if($this->ffp($path))
				{
					echo $url;
					echo $path;
				}
				break;
			case 5:				
				$time = 300;
				$path="produce.php?c=main&a=start_detection"."&time=".$time;
				if($this->ffp($path))
				{
					echo $url;
					echo $path;
				}
				break;
			case 10:				
				$time = 600;
				$path="produce.php?c=main&a=start_detection"."&time=".$time;
				if($this->ffp($path))
				{
					echo $url;
					echo $path;
				}
				break;
			case 15:				
				$time = 900;
				$path="produce.php?c=main&a=start_detection"."&time=".$time;
				if($this->ffp($path))
				{
					echo $url;
					echo $path;
				}
				break;
			case 20:				
				$time = 1200;
				$path="produce.php?c=main&a=start_detection"."&time=".$time;
				if($this->ffp($path))
				{
					echo $url;
					echo $path;
				}
				break;
			case 30:				
				$time = 1800;
				$path="produce.php?c=main&a=start_detection"."&time=".$time;
				if($this->ffp($path))
				{
					echo $url;
					echo $path;
				}
				break;
			case 60:				
				$time = 3600;
				$path="produce.php?c=main&a=start_detection"."&time=".$time;
				if($this->ffp($path))
				{
					echo $url;
					echo $path;
				}
				break;						
		}

	}
	
	
	
	
	
    function triggerRequest($url, $post_data = array(), $cookie = array()) 
	{
		$method = "GET"; //可以通过POST或者GET传递一些参数给要触发的脚本
		$url_array = parse_url ( $url ); //获取URL信息，以便平凑HTTP HEADER
		$port = isset ( $url_array [port] ) ? $url_array [port] : 80;
		
		$fp = fsockopen ( $url_array [host], $port, $errno, $errstr, 30 );
		if (! $fp) {
			return FALSE;
		}
		$getPath = $url_array [path] . "?" . $url_array [query];
		if (! empty ( $post_data )) {
			$method = "POST";
		}
		$header = $method . " " . $getPath;
		$header .= " HTTP/1.1\r\n";
		$header .= "Host: " . $url_array [host] . "\r\n "; //HTTP 1.1 Host域不能省略
		/*
		*/
		/*以下头信息域可以省略
        $header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13 \r\n";
        $header .= "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,q=0.5 \r\n";
        $header .= "Accept-Language: en-us,en;q=0.5 ";
        $header .= "Accept-Encoding: gzip,deflate\r\n";
         */
		
		$header .= "Connection:Close\r\n";
		
		if (! empty ( $cookie )) {
			$_cookie = strval ( NULL );
			foreach ( $cookie as $k => $v ) {
				$_cookie .= $k . "=" . $v . "; ";
			}
			$cookie_str = "Cookie: " . base64_encode ( $_cookie ) . " \r\n"; //传递Cookie
			$header .= $cookie_str;
		}
		if (! empty ( $post_data )) {
			$_post = strval ( NULL );
			foreach ( $post_data as $k => $v ) {
				$_post .= $k . "=" . $v . "&";
			}
			$post_str = "Content-Type: application/x-www-form-urlencoded\r\n"; //POST数据
			$post_str .= "Content-Length: " . strlen ( $_post ) . " \r\n"; //POST数据的长度
			

			$post_str .= $_post . "\r\n\r\n "; //传递POST数据
			$header .= $post_str;
		}
		fwrite ( $fp, $header );
		//echo fread($fp, 1024); //我们不关心服务器返回
		fclose ( $fp );
		return true;
	}     
    
	
	function ffp($path) {
		$fp = fsockopen ( "www.speed-1.com", 80, $errno, $errstr, 30 );
		if (! $fp) {
			echo "$errstr ($errno)<br />\n";
		} else {
			$header = "GET /$path HTTP/1.1\r\n";
			$header .= "Host: www.speed-1.com\r\n";
			$header .= "Connection: Close\r\n\r\n";
			
			fwrite ( $fp, $header );
		
			fclose ( $fp );
			echo $header;
			return true;
		}
	}

}